package com.it2297.controller;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.it2297.main.App;
import com.it2297.main.UserApp;
import com.it2297.model.Company;
import com.it2297.model.Customer;
import com.it2297.model.Needy;
import com.it2297.model.User;
import com.it2297.view.CreateCustomerView;
import com.it2297.view.LoginView;
import com.it2297.view.DeleteCustomerView;
import com.it2297.view.SearchCustomerView;
import com.it2297.view.UpdateCustomerView;
import com.it2297.view.ViewCustomerView;

import javax.swing.JOptionPane;

public class CustomerController {

	private CreateCustomerView view;
	private UpdateCustomerView updateCustomerView;
	private LoginView loginCustomerView;
	private DeleteCustomerView deleteCustomerView;
	private ViewCustomerView viewCustomerView;
	private SearchCustomerView searchCustomerView;

	public CustomerController(CreateCustomerView view) {
		this.view = view;
	}

	public CustomerController(UpdateCustomerView updateCV) {
		this.updateCustomerView = updateCV;
	}

	public CustomerController(LoginView clv) {
		this.loginCustomerView = clv;
	}

	public CustomerController(DeleteCustomerView dcv) {
		this.deleteCustomerView = dcv;
	}

	public CustomerController(ViewCustomerView vcv) {
		this.viewCustomerView = vcv;
	}

	public CustomerController(SearchCustomerView scv) {
		this.searchCustomerView = scv;
	}

	public boolean create() {
		boolean valid = false;
		boolean isSuccessful = User.isNricExist(view.getNric());
		
		if(!isSuccessful){
		
		// let the controller retrieve data from the view
		String nric = view.getNric();
		String name = view.getName();
		String address = view.getAddress();
		String gender = view.getGender();
		String COB = view.getCOB();
	//	Date DOB = view.getDOB();
		//String DOB = view.getDOB();
		//System.out.println(DOB);
		
		/*String dob = null;
		try {
			dob = df.format(DOB);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		String email = view.getEmail();
		String ethnic = view.getRace();
		String phone = view.getPhone();
		String password = view.getPassword();

		// create empty entity
		User user = new User();

		// controller set information to entity
		user.setNric(nric);
		user.setName(name);
		user.setAddress(address);
		user.setEmail(email);
		user.setPhone(phone);
		user.setStatus("ava");
		user.setPassword(password);
		user.setDob(view.getDOB());
		user.setCountryOfBirth(COB);
		user.setGender(gender);
		user.setEthnic(ethnic);
		user.setRole("CUSTOMER");
		// controller submit the entity to database
		valid = user.create(user);
		JOptionPane.showMessageDialog(null, "User successfully created!");
		}
		else {
			JOptionPane.showMessageDialog(null, "User already exists! Please try again.");
		}
		return valid;
	}

	// Delete is done
	public void delete() {

		// let the controller retrieve data from the view
		String nric = deleteCustomerView.getNric();

		// create empty entity
		User user = new User();

		// controller set information to entity
		user.setNric(nric);

		boolean isSuccessful = User.isCustomerExist(deleteCustomerView
				.getNric());

		if (isSuccessful) {
			// controller submit the entity to database
			user.delete();
			JOptionPane
					.showMessageDialog(null, "User successfully terminated!");
		} else if (!isSuccessful) {
			JOptionPane.showMessageDialog(null,
					"User does not exist, unable to delete!");
		}

	}

	public void update() {
		// create empty entity
		User user = new User();

		user.setAddress(updateCustomerView.getAddress());
		user.setEmail(updateCustomerView.getEmail());
		user.setName(updateCustomerView.getName());
		user.setPhone(updateCustomerView.getPhone());
		user.setPassword(updateCustomerView.getPassword());
		user.setNric(updateCustomerView.getNric());

		user.update(user);

		JOptionPane.showMessageDialog(null, "Record updated successfully!");
		User user2 = User.retrieveDetails(updateCustomerView.getNric());

		UserApp.setCurrentUser(user2);
	}

	// search customer and retrieve everything of that customer
	public void search() {

		User user = new User();

		boolean isSuccessful = User.isCustomerExist(searchCustomerView
				.getNric());

		if (isSuccessful) {

			user.setNric(searchCustomerView.getNric());
			user.search();

			searchCustomerView.setAddress(user.getAddress());
			searchCustomerView.setName(user.getName());
			searchCustomerView.setEmail(user.getEmail());
			searchCustomerView.setPhone(user.getPhone());
			searchCustomerView.setGender(user.getGender());
			searchCustomerView.setRace(user.getEthnic());
			searchCustomerView.setDOB(user.getDob());
			searchCustomerView.setCOB(user.getCountryOfBirth());
		} else if (!isSuccessful) {

			JOptionPane.showMessageDialog(null,
					"User does not exist. Please try again.");
			searchCustomerView.setAddress("");
			searchCustomerView.setName("");
			searchCustomerView.setEmail("");
			searchCustomerView.setPhone("");
			searchCustomerView.setGender("");
			searchCustomerView.setCOB("");
			searchCustomerView.setRace("");
			searchCustomerView.setDate();

		}
	}

	public User retrieve(String nric) {
		User user = new User();
		String nricview = viewCustomerView.getNric();
		// let the controller retrieve data from the view
		user.retrieveDetails(nricview);

		viewCustomerView.setAddress(user.getAddress());
		viewCustomerView.setName(user.getName());
		viewCustomerView.setEmail(user.getEmail());
		viewCustomerView.setPhone(user.getPhone());

		return user;

	}

	public void validateLogin() {
		String Nric = loginCustomerView.getNric();

		boolean isSuccessful = User.Login(loginCustomerView.getNric(),
				loginCustomerView.getPassword());
		if (isSuccessful) {
			User user = User.retrieveDetails(Nric);
			if (user.getRole().equals("CUSTOMER")) {
				UserApp.setCurrentUser(user);
				// JOptionPane.showMessageDialog(null,
				// "Logged in as " + user.getRole());
				App app = new App();
				app.setVisible(true);
				loginCustomerView.dispose();

			}

			else if (user.getRole().equals("NEEDY")) {

				UserApp.setCurrentUser(user);
				// JOptionPane.showMessageDialog(null,
				// "Logged in as " + user.getRole());
				App app = new App();
				app.setVisible(true);
				loginCustomerView.dispose();
			} else if (user.getRole().equals("TRAINER")) {
				UserApp.setCurrentUser(user);
				// JOptionPane.showMessageDialog(null,
				// "Logged in as " + user.getRole());
				App app = new App();
				app.setVisible(true);
				loginCustomerView.dispose();
			}

			else if (user.getRole().equals("ADMIN")) {
				UserApp.setCurrentUser(user);
				// JOptionPane.showMessageDialog(null,
				// "Logged in as " + user.getRole());
				App app = new App();
				app.setVisible(true);
				loginCustomerView.dispose();
			}

			else {
				JOptionPane.showMessageDialog(null, "Invalid login");
			}

		}

		else {
			JOptionPane.showMessageDialog(null,
					"Please try again. Username or Password is wrong.");
		}

	}

}
